package com.logic.landseaserver.ws;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import com.logic.common.enums.HttpCodeEnum;
import com.logic.common.exception.BusinessException;
import com.logic.common.util.CommonResult;
import com.logic.common.util.CommonResultMap;
import com.logic.common.util.QueryUtil;
import com.logic.landseaserver.domain.ProLine;
import com.logic.landseaserver.service.IProductLineService;
import com.logic.landseaserver.ws.dto.ProductLine4TypeDTO;
import com.logic.landseaserver.ws.dto.ProductLineDTO;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

/**
 * @Author: jack
 * @Date: 2017/5/9
 * @Description: 产品系列控制类
 */
@Path("/productLine")
@Api(value="productLine")
public class ProductLineRest {

    private Logger logger = LoggerFactory.getLogger(this.getClass());
    
	@Autowired
	private IProductLineService iProductLineService;

	//增加
	@POST
	@Path("/createProductLine")
	@Produces(MediaType.APPLICATION_JSON)
	@Consumes(MediaType.APPLICATION_JSON)
	@ApiOperation(value = "Create a new ProductLine",position = 1)
	public Response createActivity(@ApiParam(value = "param", required = true) ProductLineDTO productLineDTO) {
		CommonResultMap map = new CommonResultMap();
        try {
        	int id = iProductLineService.insertProductLine(productLineDTO);
        	productLineDTO.setId(id);
            map.setData(productLineDTO);
            map.setResult(CommonResult.SUCCESS, "Create success");
        } catch (BusinessException e) {
            logger.error("createActivity error.", e);
            map.setResult(CommonResult.BUSINESS_EXCEPTION, e.getMessage());
        } catch (Exception e) {
            logger.error("createActivity error.", e);
            map.setResult(CommonResult.ERROR, e.getMessage());
        }
        return Response.status(HttpCodeEnum.OK.getCode()).entity(map).build();
	}
	
	//查询所有信息
	@POST
	@Path("/list")
	@Produces(MediaType.APPLICATION_JSON)
	@Consumes(MediaType.APPLICATION_JSON)
	@ApiOperation(value = "list productLine",position = 2)
	public Response list(@ApiParam(value = "param", required = true) QueryUtil queryUtil){
        CommonResultMap map = new CommonResultMap();
        List<ProductLineDTO> productLineDTOList = null;
        try {
            queryUtil.getPagingTool().calculateStartIndex();
            productLineDTOList = (List<ProductLineDTO>)iProductLineService.query(queryUtil);
            Integer totalCount = queryUtil.getPagingTool().getTotalNum();
            map.setData(productLineDTOList);
            map.setResultTotalCount(totalCount);
            map.setResult(CommonResult.SUCCESS);

        } catch (Exception e) {
            logger.error("list error.", e);
            map.setResult(CommonResult.ERROR, e.getMessage());
        }
        return Response.status(HttpCodeEnum.OK.getCode()).entity(map).build();		
	}
	//删除
    @DELETE
    @Produces(MediaType.APPLICATION_JSON)
    @ApiOperation(value = "logic delete productLine by set deleted", position = 3)
    public Response logicDelete(@ApiParam(value = "param", required = true) @QueryParam("id") Integer id) {
        CommonResultMap map = new CommonResultMap();
        try {
        	iProductLineService.deleteProductLine(id);
            map.setResult(CommonResult.SUCCESS,"delete productLine");
        } catch (Exception e) {
            logger.error("logicDelete error.", e);
            map.setResult(CommonResult.ERROR, e.getMessage());
        }
        return Response.status(HttpCodeEnum.OK.getCode()).entity(map).build();
    }
	
    //编辑
    @PUT
	@Path("/editProductLine")
	@Produces(MediaType.APPLICATION_JSON)
	@Consumes(MediaType.APPLICATION_JSON)
	@ApiOperation(value = "Edit ProductLine ")
    public Response updateOrder(@ApiParam(value = "param", required = true) ProductLineDTO productLineDTO) {
        CommonResultMap map = new CommonResultMap();
        try {
        	iProductLineService.editProductLine(productLineDTO);
            map.setResult(CommonResult.SUCCESS,"edit ProductLine");
        } catch (Exception e) {
            logger.error("updateOrder error.", e);
            map.setResult(CommonResult.ERROR, e.getMessage());
        }
        return Response.status(HttpCodeEnum.OK.getCode()).entity(map).build();
    }
    
	//根据类型进行查询
	@POST
	@Path("/getProductLineForFlag")
	@Produces(MediaType.APPLICATION_JSON)
	@Consumes(MediaType.APPLICATION_JSON)
	@ApiOperation(value = "产品系列，网站和微信端   参数格式{flag:xx,xx}",position = 4)
	public Response getAcctivityForFlag(@ApiParam(value = "param", required = true) Map pmap) {
	     CommonResultMap map = new CommonResultMap();
	     Map<String, Object> result = new HashMap<String, Object>();
	     try {
	    	  List<ProductLine4TypeDTO> projects =  iProductLineService.getProductLineListForFlag((String)pmap.get("flag"));
	          
	    	  List<ProLine> proLine = iProductLineService.getProductLineAll();
	    	  result.put("total", projects.size());
	    	  result.put("proLine", proLine);
        	  result.put("projects", projects);
              map.setData(result);
        } catch (Exception e) {
            logger.error("getAcctivityForFlag error.", e);
        }
        return Response.status(HttpCodeEnum.OK.getCode()).entity(map).build();		
	}
}
